package com.glory.wxmemsicpda.activity

import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.adapter.DataAcquisitionAdapter
import com.glory.wxmemsicpda.base.App
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.DataAcquisitionBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_data_acquisition.*

class DataAcquisitionActivity : BaseDialogActivity() {


//    private var viewDataBinding: ActivityCheckTableBinding? = null
    private var eqpId: String = ""
    private var dataAcqBean :DataAcquisitionBean = DataAcquisitionBean()
    private var dataAcqList: MutableList<DataAcquisitionBean.EDCSETLISTDTO> = mutableListOf()
    private var dataAcqAdapterList: MutableList<DataAcquisitionBean.EDCSETLISTDTO.EDCSETBINLINELISTDTO> = mutableListOf()
    private var dataAcqAdapter: DataAcquisitionAdapter? = null
    private var tipDialog: FreeUI_GeneralFragmentDialog? = null

    override fun getLayoutResId(): Int {
        return R.layout.activity_data_acquisition
    }

    override fun initView() {

//        viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_data_acquisition)

        setTitle("数据采集")
        freeUI_titleView.setRightListener("保存采集") {
            requestSaveAcquisition()
        }
        eqpId = intent.getStringExtra("showPlugin")!!

        dataAcqAdapter = DataAcquisitionAdapter(dataAcqAdapterList,quaNum,undesirableNum,0)

        dataAcq_recycler.adapter = dataAcqAdapter


    }

    override fun getBaseEntity(): FreeUI_AddViewEntity? {
        return FreeUI_AddViewEntity("header", llMain)
    }



    override fun initData() {

        val map = JsonObject()
        map.addProperty("EQPID",eqpId)
        NetUtils.requestPosNet(this,"GETEDCCURRENT","MES.LOTABORT",
            map,object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<DataAcquisitionBean?>?>(this) {
                override fun onSuccess(response: Response<BaseResponseBean<DataAcquisitionBean?>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.response.getHeader().getRESULT().toUpperCase() == "SUCCESS") {
                        if (response.body()!!.response.body !== null){
                            dataAcqList.clear()
                            dataAcqBean = response.body()!!.response.body!!
                            eqp_id.text = dataAcqBean.eqpid
                            lot_id.text = dataAcqBean.lotid
                            total_num.text = dataAcqBean.mainqty
                            quaNum.text = dataAcqBean.mainqty
                            undesirableNum.text = "0"
                            if (response.body()!!.response.body!!.edcsetlist.size>0){
                                dataAcqList.addAll(response.body()!!.response.body!!.edcsetlist)
                                if (dataAcqBean.edcsetlist.get(0).edcsetbinlinelist.size>0){
                                    dataAcqAdapterList.addAll(dataAcqBean.edcsetlist.get(0).edcsetbinlinelist)
                                }
                            }
                            dataAcqAdapter?.setMainQty(dataAcqBean.mainqty)
                            dataAcqAdapter?.notifyDataSetChanged()
                        }
                    }else{
                        tipDialog = FreeApi_DialogUtils.getConfirmDialog(
                            App.app, "确定", response.body()!!.response.getHeader().getRESULTMESSAGE()
                        ) {
                            finish()
                        }
                        tipDialog!!.show(supportFragmentManager, "DIALOG")
                    }
                }

                override fun onError(response: Response<BaseResponseBean<DataAcquisitionBean?>?>) {
                    super.onError(response)
                    loadError(response.exception, "GETEDCCURRENT")
                }
            })

    }




    private fun requestSaveAcquisition(){

        val map = JsonObject()
        map.addProperty("EQPID", eqpId)
        map.addProperty("LOTID", dataAcqBean.lotid)
        map.addProperty("MAINQTY", dataAcqBean.mainqty)
        map.addProperty("PARTNAME", dataAcqBean.partname)
        map.addProperty("STEPNAME", dataAcqBean.stepname)
        if (dataAcqList.size>0){
            val edcSetList = JsonArray()
            val edcSet = JsonObject()
            edcSet.addProperty("EDCSETNAME",dataAcqList.get(0).edcsetname)
            edcSet.addProperty("EDCSETDESCRIPTION",dataAcqList.get(0).edcsetdescription)
            edcSet.addProperty("EDCTYPE",dataAcqList.get(0).edctype)
            val edcSetBinLineList = JsonArray()
            var data = dataAcqAdapter!!.data
            data.forEach {
                val binLine = JsonObject()
                binLine.addProperty("NAME",it?.name)
                binLine.addProperty("VALUE",it?.value)
                binLine.addProperty("DESCRIPTION",it?.description)
                binLine.addProperty("SPECTYPE",it?.spectype)
                binLine.addProperty("BINTYPE",it?.bintype)
                binLine.addProperty("BINACTION",it?.binaction)
                edcSetBinLineList.add(binLine)
            }
            edcSet.add("EDCSETBINLINELIST",edcSetBinLineList)
            edcSetList.add(edcSet)
            map.add("EDCSETLIST",edcSetList)
        }
        NetUtils.requestPosNet(this,
            "SAVEEDCDATA", "MES.LOTABORT",
            map,
            object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<*>?>(this) {
                override fun onSuccess(response: Response<BaseResponseBean<*>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(false, supportFragmentManager)) {
//                        showToast("批次开始成功", true)
                        showMessageDialog(
                            "保存成功！",
                            supportFragmentManager
                        )
//                        finish()
                    }
                }

                override fun onError(response: Response<BaseResponseBean<*>?>) {
                    super.onError(response)
                    loadError(response.exception, "SAVEEDCDATA")
                }
            })


    }


    //消息提示框
    private fun showMessageDialog(message: String, fragmentManager: FragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
            App.app, "确定", message
        ) {
            finish()
        }.show(fragmentManager, "DIALOG")

    }


}